package com.link.cloud.config.mybatis.typeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.time.LocalTime;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

/**
 * LocalTime与java.sql.time数据类型转换
 * <br>
 * 自定义mybatis的数据类型转换
 * <br>
 * mybatis的配置时，设置TypeHandlersPackage
 * @author maxiaob
 *
 */
@MappedTypes(LocalTime.class)
public class LocalTimeHandler extends BaseTypeHandler<LocalTime>{

	@Override
	public LocalTime getNullableResult(ResultSet rs, String cloumnName) throws SQLException {
		Time time = rs.getTime(cloumnName);
		if(time == null){
			return null;
		}
		return time.toLocalTime();
	}

	@Override
	public LocalTime getNullableResult(ResultSet rs, int cloumnIndex) throws SQLException {
		Time time = rs.getTime(cloumnIndex);
		if(time == null){
			return null;
		}
		return time.toLocalTime();
	}

	@Override
	public LocalTime getNullableResult(CallableStatement cs, int cloumnIndex) throws SQLException {
		Time time = cs.getTime(cloumnIndex);
		if(time == null){
			return null;
		}
		return time.toLocalTime();
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, LocalTime localTime, JdbcType jdbcType)
			throws SQLException {
		ps.setTime(i, Time.valueOf(localTime));
	}

}
